// pages/index/clearCar/clearCarDetail/clearCarDetail.js
const app = getApp()
const request = require('../../../utils/request')
import parser from 'rich-text-parser'

Page({
  data: {
    indicatorDots: true,  //是否显示面板指示点
    autoplay: true,      //是否自动切换
    interval: 3000,       //自动切换时间间隔
    duration: 1000,       //滑动动画时长
    goods_id: 0,
    circular: true,
    goods_id: 0,
    price: 0,
    collect: 0,
    title: "",
    detail: ""
  },
  onLoad: function (options) {
    this.setData({
      goods_id: options.goods_id
    })
    this.getDetail();

  },
  getDetail() {
    wx.showLoading()
    const data = {
      id: this.data.goods_id,
    }
    request.get('goods_detail', data)
      .then(res => {
        wx.hideLoading()
        const nodes = parser.getRichTextJson(res.data.detail)
        this.setData({
          imgs: res.data.imgs,
          price: res.data.price,
          title: res.data.title,
          detail: nodes.children,
          goods_id: res.data.id,
          collect: res.data.is_collect
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },
  // 加入购物车
  addCart(){
    var that = this;
    wx.showLoading({
      title: '',
    })
    wx.request({
      url: app.d.severUrl + 'addCart',
      method: 'GET',
      data: {
        token: wx.getStorageSync("token"),
        goods_id: that.data.goods_id,
        goods_num: 1,
      },
      success: res => {
        wx.hideLoading();
        if (res.data.code == 0) {
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })

        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })
        }
      },
      fail: error => {
        wx.showToast({
          icon: 'none',
          title: "网络错误，请稍后重试"
        })
      }
    })
  },
  toCart(){
    wx.navigateTo({
      url: '/pages/market/shoppingCar/shoppingCar',
    })
  },
  // 立即购买
  toBuy(){
    wx.showLoading()
    const data = {
      token: wx.getStorageSync("token"),
      goods_id: this.data.goods_id,
    }
    request.get('buy_now', data)
      .then(res => {
        wx.hideLoading()
        wx.showToast({
          icon: 'none',
          title: res.msg
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },
  //收藏
  toCollect(){
    wx.showLoading()
    const data = {
      token: wx.getStorageSync("token"),
      goods_id: this.data.goods_id,
    }
    request.get('collect', data)
      .then(res => {
        wx.hideLoading()
        this.setData({
          collect: 1 // TODO: 此处后台应该返回不同的收藏状态，但是现在收藏过的商品只提示已收藏，没有进行状态的改变，也没有返回状态
        })
        
        wx.showToast({
          icon: 'none',
          title: res.msg
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },
  // 客服
  toService(){
    wx.navigateTo({
      url: '/pages/userCenter/server/server',
    })
  }
})